/*
   <GameWorld>
            <Scenery  type = "image">                         Mountains  </Scenery>
			<Scenery  type = "tiled">                         Ground     </Scenery>
   
			
			<Entity   x = "??" y = "??" args = "??,??">  colonist_32_32  </Entity>
			<Entity   x = "??" y = "??" args = "??,??">  colonist_32_32  </Entity>
			<Entity   x = "??" y = "??" args = "??,??">  colonist_32_32  </Entity>
			<Entity   x = "??" y = "??" args = "??,??">  colonist_32_32  </Entity>
			
			<Entity   x = "??" y = "??" args = "??,??">  player          </Entity>
			
			<Entity   x = "??" y = "??" args = "??,??">  zombie          </Entity>
			<Entity   x = "??" y = "??" args = "??,??">  zombie          </Entity>
			<Entity   x = "??" y = "??" args = "??,??">  zombie          </Entity>
			<Entity   x = "??" y = "??" args = "??,??">  zombie          </Entity>
			<Entity   x = "??" y = "??" args = "??,??">  zombie          </Entity>
   </GameWorld>
 */ 
package pure.engine.world 
{
	import flash.geom.*;
	import pure.debug.Logger;
	import pure.engine.core.ns_despair;

	use namespace ns_despair;
	
    /**
     * 世界管理器
	 * [Param] - PURE
	 * 
	 * [Hint] - Mediator模式实现，管理WorldBody与Monitor，并负责其之间的交互.
     */
public class WorldManager
{

	
	/**
	 * 注册世界主体
	 * 
	 * [Param] - bodyName           主体名字
	 * [Param] - regionSize         区块边长
	 * [Param] - viewW              (视域 × 渲染)宽度
	 * [Param] - viewW              (视域 × 渲染)高度
	 * [Param] - numExtendedRegion  延伸区块数量
	 * [Param] - illuminatedEntire  完全照明
	 * [Param] - sortProperty       排序属性列表
	 * [Return] - 世界主体
	 */
	ns_despair static function registerWorldBody(bodyName:String, regionSize:int, viewW:int, viewH:int, numExtendedRegion:int, optimized:Boolean):WorldBody
	{
		var worldBody:WorldBody;
		var monitor:Monitor;
		
		if (bodyName == null || bodyName == '')
		{
			throw new Error("[WorldManager] - ＃registerWorldBody - A world body does not have a null name.");
		}
		
		if (!m_worldBodyList)
		{
			m_worldBodyList = { };
		}
		else if (m_worldBodyList[bodyName])
		{
			throw new Error("[WorldManager] - ＃getWorldBody - world body ( "+bodyName+" ) has been registered.");
		}
		
		if(!m_monitorList)
		{
			m_monitorList = { };
		}
		
		worldBody = m_worldBodyList[bodyName] = new WorldBody(bodyName, regionSize, viewW, viewH, numExtendedRegion, optimized);
		monitor = m_monitorList[bodyName] = new Monitor();
		monitor.initialize(bodyName, viewW , viewH);
		worldBody.m_modifiedModelList = monitor.m_modifiedModelList = [];
		
		trace('\n----------------------------------- [WorldBody] -----------------------------------');
		Logger.reportMessage('WorldManager', '世界主体 ( ' + bodyName + ' ) 注册完成...');
		Logger.reportMessage('WorldManager', '视域属性: ' + (optimized ? '区域照明' : '全局照明') + ' × 区块尺寸(' + regionSize + ')' + ' × 延伸区块数量(' + numExtendedRegion +')');
		Logger.reportMessage('WorldManager', '视域范围: ' + viewW + '×' + viewH);
		trace('-----------------------------------------------------------------------------------');
		
		return worldBody;
	}
	
	
	/**
	 * 获取世界主体
	 * 
	 * [Param] - bodyName
	 * [Return] - 世界主体
	 */
	ns_despair static function getWorldBody(bodyName:String):WorldBody
	{
		if (!m_worldBodyList)
		{
			throw new Error("[WorldManager] - ＃getWorldBody - There has been no world body registered yet.");
		}

		if (!m_worldBodyList[bodyName])
		{
			throw new Error("[WorldManager] - ＃getWorldBody - The world body ( "+bodyName+" ) is not existed.");
		}
		
		return m_worldBodyList[bodyName];
	}

	
	/**
	 * 进入世界
	 */
	ns_despair static function enterWorld(bodyName:String):void
	{
		m_worldBodyList[bodyName].enterWorld();
		m_monitorList[bodyName].enterWorld();
	}
	
	
	/**
	 * 退出世界
	 */
	ns_despair static function exitWorld(bodyName:String):void
	{
		m_worldBodyList[bodyName].exitWorld();
		m_monitorList[bodyName].exitWorld();
	}
	
	
	/**
	 * 完成退出
	 */
	ns_despair static function finishExit(bodyName:String):void
	{
		m_worldBodyList[bodyName].finishExit();
		m_monitorList[bodyName].finishExit();
	}
	
	
	
	
	//======================
	// Member
	//======================
	
	
	ns_despair static var m_worldBodyList:Object;
	
	ns_despair static var m_monitorList:Object;

}
}